package com.example.demo.controller;

import com.example.demo.core.Result;
import com.example.demo.entity.OnlineProject;
import com.example.demo.pojo.request.CreateOnlineProjectRequest;
import com.example.demo.service.OnlineProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 网签项目配置控制器
 *
 * @author 叮当猫的百宝箱
 * @since 1.0
 */
@Tag(name = "网签项目配置管理", description = "网签项目配置管理")
@RestController
@RequestMapping("/online")
@RequiredArgsConstructor
public class OnlineProjectController {

    private final OnlineProjectService onlineProjectService;


    @Operation(summary = "添加网签配置")
    @PreAuthorize("hasAuthority('/online/create')")
    @PostMapping("/project/create")
    public Result<Void> createOnlineProject(@RequestBody @Validated CreateOnlineProjectRequest onlineProjectRequest) {
        OnlineProject onlineProject = new OnlineProject();
        BeanUtils.copyProperties(onlineProjectRequest, onlineProject);
        onlineProjectService.createOnlineProject(onlineProject);
        return Result.ok();
    }

    /**
     * 根据网签配置ID获取，网签配置下的所有网签配置项目信息
     *
     * @param onlineId 网签配置ID
     * @return API全局响应对象
     */
    @GetMapping("/{onlineId}/project")
    @Operation(summary = "根据网签配置ID获取网签项目列表")
    @PreAuthorize("hasAuthority('/online/show')")
    public Result<List<OnlineProject>> getOnlineProjectByOnlineId(@PathVariable("onlineId") Long onlineId) {
        List<OnlineProject> onlineProjectList = onlineProjectService.getOnlineProjectByOnlineId(onlineId);
        return Result.ok(onlineProjectList);
    }

    /**
     * 根据网签项目ID删除网签项目配置
     *
     * @param id 网签配置ID
     * @return API全局响应对象
     */
    @PostMapping("/project/{onlineProjectId}/delete")
    @Operation(summary = "根据网签项目ID删除网签项目配置")
    @PreAuthorize("hasAuthority('/online/delete')")
    public Result<Void> deleteOnlineProjectById(@PathVariable("onlineProjectId") Long id) {
        onlineProjectService.deleteOnlineProjectById(id);
        return Result.ok();
    }


}
